package com.highcom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.highcom.pojo.Dept;
import com.highcom.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("/bumenguanli")
    public ModelAndView findAllDepts(@RequestParam(defaultValue = "1",required = true,value = "pageNo") Integer pageNo){
        ModelAndView m = new ModelAndView();
        //开始分页
        PageHelper.startPage(pageNo,3);
        List<Dept> deptsList = deptService.findDepts();
        //把要分页的集合数据添加到PageInfo中
        PageInfo<Dept> pageInfo = new PageInfo<>(deptsList);
        m.addObject("deptInfo", pageInfo);
        m.setViewName("/files/bumenguanli.jsp");
        return m;
    }

    @RequestMapping("/delDeptById")
    public String delDeptById(int id) {
        System.out.println(id);
        deptService.delDeptById(id);
        return "redirect:/bumenguanli";
    }

    @RequestMapping("/delDeptsByids")
    public String delRolesByIds(int[] ids){
        System.out.println(ids);
        deptService.delDeptsByIds(ids);
        return "redirect:/bumenguanli";
    }


    @RequestMapping("/findDeptById")
    public ModelAndView findDeptById(int id){
        ModelAndView m = new ModelAndView();
        Dept deptById = deptService.findDeptById(id);
        m.addObject("deptInfo",deptById);
        m.setViewName("/files/bumenmodify.jsp");
        return m;
    }

    @RequestMapping("/modifyDept")
    public String modifyDept(Dept dept){
        System.out.println(dept.getId());
        deptService.modifyDept(dept);
        return "redirect:/bumenguanli";
    }


    @RequestMapping("/addDept")
    public String addRole(Dept dept) {
        deptService.addDept(dept);
        return "redirect:/bumenguanli";
    }

    @RequestMapping("/queryDept")
    public ModelAndView findRolesLikeQuery(Dept dept,@RequestParam(defaultValue = "1",required = true,value = "pageNo") Integer pageNo) {
        System.out.println(dept.toString());
        ModelAndView m = new ModelAndView();
        List<Dept> deptsLikeQuery = deptService.findDeptsLikeQuery(dept);
        //把要分页的集合数据添加到PageInfo中
        PageInfo<Dept> pageInfo = new PageInfo<>(deptsLikeQuery);
        m.addObject("deptInfo", pageInfo);
        m.setViewName("/files/bumenguanli.jsp");
        return m;
    }

}
